
angular.module('module.auth', [
    "ngCookies"
])

    .config(['$stateProvider',   '$urlRouterProvider',
        function($stateProvider, $urlRouterProvider) {
            $stateProvider
              /*  .state('register', {
                    url : '/register/:status',
                    templateUrl : 'view/login/register.html',
                    resolve:{
                        status:['$stateParams',function($stateParams){
                            return $stateParams.status;
                        }]
                    },
                    controller : 'RegisterController'
                })*/
                .state('merchmsg', {
                    url : '/merchmsg',
                    templateUrl : 'view/login/merchmsg.html',
                    controller : 'MerchmsgController'
                })
        }])
/*
    .controller('RegisterController', ['$scope', '$state','$stateParams','$cookieStore','AuthService','status',function($scope,$state, $stateParams,$cookieStore,AuthService,status) {
        $scope.status = status;
        $("#title").html("填写商户基本资料");
        dismiss();
        $scope.banks = bank_base;
        $scope.merchant = {};
        $scope.merchant.acctBankname = "民生银行";
        $scope.register = function ($event) {
            loadCart(true,"loader",".loader_bac");
            fileslen = 0;
            now = 0;
            var self = $event.target;
            self.setAttribute("disabled","disabled");
            self.style.backgroundColor = "#ccc";
            var isNone = false;
            for(var name in $scope.filesList){
                isNone = true;
                fileslen++;
                imgUpload(name,$scope.filesList[name],$event);
            }
            if(!isNone){
                register($event);
            }
        };
        function register($event){
            $scope.user = $cookieStore.get("_user");
            $scope.addswMerchant($event);
            $scope.addMerchant($scope.user);
        }
        //添加数网商户
        $scope.addswMerchant = function($event){
            var self=$event.target;
            var url = window.location.href;
            var reg = new RegExp("(^|&)" + "agent" + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            var param = "";
            if (r) {
                param = unescape(r[2]);//微信截取code函数
            }
            $scope.merchant.legalPhone=$scope.user.phone;
            $scope.merchant.userid=0;
            $scope.merchant.settleType=2;
            $scope.merchant.basicRate=0.35;
            $scope.merchant.inviteCode = param;
            AuthService.receiptRegister($scope.merchant, function (data){
                //添加新外卖平台商户
                $scope.merchid = data.merchid;
                $scope.addMerchant($scope.user);
                window.location.href="http://www.shuwang.info/agent/merchpending/?merchid="+param;
                loadCart(false,"loader",".loader_bac");
            },function(result){
                if(result.data){
                    if(result.data.result){
                        alert(result.data.result);
                    }
                }else{
                    alert(JSON.stringify(result));
                }
                self.removeAttribute("disabled","disabled");
                self.style.backgroundColor = "#09b101";
                loadCart(false,"loader",".loader_bac");
            })
        };
        //添加外卖商户
        $scope.addMerchant = function(user){
            var merchant = {
                userid:user.userid,
                phone:user.phone,
                name:$scope.merchant.merchname,
                address:$scope.merchant.address,
                licenseName :"营业执照",
                licenseId :"",
                licenseUrl :$scope.merchant.licencePicurl,
            };
            AuthService.registerMerchant(merchant, function (data){
               //
            })
        };
        $scope.filesList = {};//存储files对象
        //在界面显示要上传的多图片
        function setImage(){
            var imageInput = document.getElementsByName("upload");
            for(var i=0;i<imageInput.length;i++){
                var fileupload = imageInput[i];
                fileupload.onchange = function(){
                    var self = this;
                    var file = this.files[0];
                    var imageType = /image.*!/;
                    if(!file.type.match(imageType)){
                        input_alert("请选择正确的文件格式", 2000);
                    }
                    var reader = new FileReader();
                    if (file.size > 10*1024*1024) {
                        input_alert("上传图片最大为10M", 2000);
                        return;
                    }
                    reader.onload = function(){
                        var parentDiv = self.parentNode;
                        var img_show_tag = parentDiv.querySelector(".img_show");
                        if(img_show_tag){
                            img_show_tag.outerHTML = "";
                        }
                        var imageType = self.getAttribute("image-type");
                        switch (imageType){
                            case "headerPicurl":
                                $scope.filesList.headerFile=file;
                                break;
                            case "licencePicurl":
                                $scope.filesList.licenceFile=file;
                                break;
                            case "legalIdfrontpic":
                                $scope.filesList.legalfrontFile=file;
                                break;
                            case "legalIdbackpic":
                                $scope.filesList.legalbackFile=file;
                                break;
                            case "acctPhoto":
                                $scope.filesList.acctFile=file;
                                break;
                        }
                        parentDiv.innerHTML+='<img class="img_show" src='+reader.result+'>';
                        setImage();
                    };
                    reader.readAsDataURL(file);
                }
            }
        }
        setImage();
        //上传图片操作
        var fileslen = 0;
        var now = 0;
        function imgUpload(filename,file,$event){
            //var url = "http://114.55.90.31:81/ngbuc/fileupload/picupload/json/";
            var url = "http://sfcommon.test.shuwang.info/fileupload/upload/1001";
            //var timestamp=new Date().getTime();
            var self = $event.target;
            var formdata = new FormData();
            formdata.append("file",file,file.name);
            formdata.append("bizcode","merchreg");
            jQuery.ajax({
                 url : url,
                 type : 'post',
                 data : formdata,
                 cache : false,
                 contentType : false,
                 processData : false,
                 dataType : "json",
                 success : function(imgurl) {
                     switch (filename){
                         case "headerFile":
                             $scope.merchant.headerPicurl=imgurl.faddress;
                             break;
                         case "licenceFile":
                             $scope.merchant.licencePicurl=imgurl.faddress;
                             break;
                         case "legalfrontFile":
                             $scope.merchant.legalIdfrontpic=imgurl.faddress;
                             break;
                         case "legalbackFile":
                             $scope.merchant.legalIdbackpic=imgurl.faddress;
                             break;
                         case "acctFile":
                             $scope.merchant.acctPhoto=imgurl.faddress;
                             break;
                     }
                     now++;
                     if(now == fileslen){
                         register($event);
                     }
                 },
                error:function(){
                    alert("图片上传失败");
                    self.removeAttribute("disabled","disabled");
                    self.style.backgroundColor = "#09b101";
                    loadCart(false,"loader",".loader_bac");
                }
             });
        }

      //地址选择
        $scope.provinceList = [];
        $scope.cityList = [];
        $scope.countyList = [];
        var district = {
            province: '',
            city: '',
            county: ''
        }
        var districtChoose = $("#districtChoose").find(".districtChoose");
        var districtId = '';
        $scope.newadr_hide = function (text) {
            if(districtId!=text){
                $("#chooseAddr").html('<div id="province" class="choose active" onclick="chooseDistrict(this,1)"> <span>请选择</span> </div>');
            }
            districtId = text;
            click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
            var getProvince = AuthService.getProvince().$promise.then(function (data) {
                $scope.provinceList = data;
                districtChoose.hide();
                districtChoose[0].style.display = 'block';
            },function(){
                getProvince;
            });
            $scope.newaddress = {};
            $scope.chooseProvince = function (province) {
                district.province = province.name;
                district.city = '';
                district.county = '';
                if (district.province == '台湾省' || district.province == '香港特别行政区' || district.province == '澳门特别行政区') {
                    $("#"+districtId).html(district.province);
                    $("#province").find('span')[0].innerHTML = province.name + "";
                    $scope.newaddress.area = district.province;
                    click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
                } else {
                    var name = district.province + district.city + district.county;
                    $("#province").find('span')[0].innerHTML = province.name + "";
                    AuthService.getCity(province.provinceid).$promise.then(function (data) {
                        $scope.cityList = data;
                        $("#chooseAddr").html('<div id="province" class="choose" onclick="chooseDistrict(this,1)"> <span>' + district.province + '</span> </div><div id="city" class="choose active" onclick="chooseDistrict(this,2)"><span>请选择</span></div>');
                        districtChoose.hide();
                        districtChoose[1].style.display = 'block';
                    })
                }
            };
            $scope.chooseCity = function (city) {
                district.city = city.name;
                district.county = '';
                var name = district.province + district.city + district.county;
                $("#"+districtId).html(name);
                $("#city").find('span')[0].innerHTML = city.name + "";
                AuthService.getCounty(city.cityid).$promise.then(function (data) {
                    $scope.countyList = data;
                    $("#chooseAddr").html('<div id="province" class="choose" onclick="chooseDistrict(this,1)"> <span>' + district.province + '</span> </div><div id="city" class="choose" onclick="chooseDistrict(this,2)"><span>' + district.city + '</span></div><div id="county" class="choose active" onclick="chooseDistrict(this,3)"><span>请选择</span></div>');
                });
                districtChoose.hide();
                districtChoose[2].style.display = 'block';
            };
            $scope.chooseCounty = function (county) {
                district.county = county.name;
                var name = district.province + district.city + district.county;
                $("#"+districtId).html(name);
                $("#county").find('span')[0].innerHTML = county.name + "";
                click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval")
                $("#"+districtId).parentNode;
                var placeholderColor = document.getElementById(districtId).parentNode.querySelector(".placeholder_color");
                $(placeholderColor).removeClass("placeholder_color ");
                if(districtId=="district"){
                    $scope.merchant.province = district.province;
                    $scope.merchant.city = district.city;
                    $scope.merchant.county = district.county;
                }else if(districtId=="district1"){
                    $scope.merchant.acctProvince = district.province;
                    $scope.merchant.acctCity = district.city;
                }
            }
        };

    }])
*/
    .controller('MerchmsgController', ['$scope', '$state','$stateParams','$cookieStore','AuthService',function($scope,$state, $stateParams,$cookieStore,AuthService) {

        console.log($scope.status)
        $("#title").html("填写商户基本资料");
        dismiss();
        $scope.banks = bank_base;
        $scope.merchant = {};
        $scope.merchant.acctBankname = "民生银行";
        $scope.register = function ($event) {
            loadCart(true,"loader",".loader_bac");
            fileslen = 0;
            now = 0;
            var self = $event.target;
            self.setAttribute("disabled","disabled");
            self.style.backgroundColor = "#ccc";
            var isNone = false;
            for(var name in $scope.filesList){
                isNone = true;
                fileslen++;
                imgUpload(name,$scope.filesList[name],$event);
            }
            if(!isNone){
                register($event);
            }
        };
        function register($event){
            $scope.user = $cookieStore.get("_user");
            $scope.addMerchant($scope.user);
            $scope.addswMerchant($event);
        }
        //添加数网商户
        $scope.addswMerchant = function($event){
            var self=$event.target;
            var url = window.location.href;
            var reg = new RegExp("(^|&)" + "agent" + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            var param = "";
            if (r) {
                param = unescape(r[2]);//微信截取code函数
            }
            $scope.merchant.legalPhone=$scope.user.phone;
            $scope.merchant.userid=0;
            $scope.merchant.settleType=2;
            $scope.merchant.basicRate=0.35;
            $scope.merchant.inviteCode = param;
            alert("该功能尚未完善，后续将继续跟进！")
           /* AuthService.receiptRegister($scope.merchant, function (data){
                //alert("信息提交成功，等待审核");
                window.location.href="http://inner.test.shuwang.info/agent/merchpending/?merchid="+param;
                loadCart(false,"loader",".loader_bac");
            },function(result){
                if(result.data){
                    if(result.data.result){
                        alert(result.data.result);
                    }
                }else{
                    alert(JSON.stringify(result));
                }
                self.removeAttribute("disabled","disabled");
                self.style.backgroundColor = "#09b101";
                loadCart(false,"loader",".loader_bac");
            })*/
        };
        //添加商户
        $scope.addMerchant = function(user){
            var merchant = {
                userid:user.userid,
                phone:user.phone,
                name:$scope.merchant.name,
                licenseName :"营业执照",
                licenseId :"",
                licenseUrl :$scope.merchant.licencePicurl,
            }
            AuthService.registerMerchant(merchant, function (data){
            })
        };
        $scope.filesList = {};//存储files对象
        //在界面显示要上传的多图片
        function setImage(){
            var imageInput = document.getElementsByName("upload");
            for(var i=0;i<imageInput.length;i++){
                var fileupload = imageInput[i];
                fileupload.onchange = function(){
                    var self = this;
                    var file = this.files[0];
                    var imageType = /image.*/;
                    if(!file.type.match(imageType)){
                        input_alert("请选择正确的文件格式", 2000);
                    }
                    var reader = new FileReader();
                    if (file.size > 10*1024*1024) {
                        input_alert("上传图片最大为10M", 2000);
                        return;
                    }
                    reader.onload = function(){
                        var parentDiv = self.parentNode;
                        var img_show_tag = parentDiv.querySelector(".img_show");
                        if(img_show_tag){
                            img_show_tag.outerHTML = "";
                        }
                        var imageType = self.getAttribute("image-type");
                        switch (imageType){
                            case "headerPicurl":
                                $scope.filesList.headerFile=file;
                                break;
                            case "licencePicurl":
                                $scope.filesList.licenceFile=file;
                                break;
                            case "legalIdfrontpic":
                                $scope.filesList.legalfrontFile=file;
                                break;
                            case "legalIdbackpic":
                                $scope.filesList.legalbackFile=file;
                                break;
                            case "acctPhoto":
                                $scope.filesList.acctFile=file;
                                break;
                        }
                        parentDiv.innerHTML+='<img class="img_show" src='+reader.result+'>';
                        setImage();
                    };
                    reader.readAsDataURL(file);
                }
            }
        }
        setImage();
        //上传图片操作
        var fileslen = 0;
        var now = 0;
        function imgUpload(filename,file,$event){
            //var url = "http://114.55.90.31:81/ngbuc/fileupload/picupload/json/";
            var url = "http://sfcommon.test.shuwang.info/fileupload/upload/1001";
            //var timestamp=new Date().getTime();
            var self = $event.target;
            var formdata = new FormData();
            formdata.append("file",file,file.name);
            formdata.append("bizcode","merchreg");
            jQuery.ajax({
                url : url,
                type : 'post',
                data : formdata,
                cache : false,
                contentType : false,
                processData : false,
                dataType : "json",
                success : function(imgurl) {
                    switch (filename){
                        case "headerFile":
                            $scope.merchant.headerPicurl=imgurl.faddress;
                            break;
                        case "licenceFile":
                            $scope.merchant.licencePicurl=imgurl.faddress;
                            break;
                        case "legalfrontFile":
                            $scope.merchant.legalIdfrontpic=imgurl.faddress;
                            break;
                        case "legalbackFile":
                            $scope.merchant.legalIdbackpic=imgurl.faddress;
                            break;
                        case "acctFile":
                            $scope.merchant.acctPhoto=imgurl.faddress;
                            break;
                    }
                    now++;
                    if(now == fileslen){
                        register($event);
                    }
                },
                error:function(){
                    alert("图片上传失败");
                    self.removeAttribute("disabled","disabled");
                    self.style.backgroundColor = "#09b101";
                    loadCart(false,"loader",".loader_bac");
                }
            });
        }

        //地址选择
        $scope.provinceList = [];
        $scope.cityList = [];
        $scope.countyList = [];
        var district = {
            province: '',
            city: '',
            county: ''
        }
        var districtChoose = $("#districtChoose").find(".districtChoose");
        var districtId = '';
        $scope.newadr_hide = function (text) {
            if(districtId!=text){
                $("#chooseAddr").html('<div id="province" class="choose active" onclick="chooseDistrict(this,1)"> <span>请选择</span> </div>');
            }
            districtId = text;
            click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
            var getProvince = AuthService.getProvince().$promise.then(function (data) {
                $scope.provinceList = data;
                districtChoose.hide();
                districtChoose[0].style.display = 'block';
            },function(){
                getProvince;
            });
            $scope.newaddress = {};
            $scope.chooseProvince = function (province) {
                district.province = province.name;
                district.city = '';
                district.county = '';
                if (district.province == '台湾省' || district.province == '香港特别行政区' || district.province == '澳门特别行政区') {
                    $("#"+districtId).html(district.province);
                    $("#province").find('span')[0].innerHTML = province.name + "";
                    $scope.newaddress.area = district.province;
                    click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
                } else {
                    var name = district.province + district.city + district.county;
                    $("#province").find('span')[0].innerHTML = province.name + "";
                    AuthService.getCity(province.provinceid).$promise.then(function (data) {
                        $scope.cityList = data;
                        $("#chooseAddr").html('<div id="province" class="choose" onclick="chooseDistrict(this,1)"> <span>' + district.province + '</span> </div><div id="city" class="choose active" onclick="chooseDistrict(this,2)"><span>请选择</span></div>');
                        districtChoose.hide();
                        districtChoose[1].style.display = 'block';
                    })
                }
            };
            $scope.chooseCity = function (city) {
                district.city = city.name;
                district.county = '';
                var name = district.province + district.city + district.county;
                $("#"+districtId).html(name);
                $("#city").find('span')[0].innerHTML = city.name + "";
                AuthService.getCounty(city.cityid).$promise.then(function (data) {
                    $scope.countyList = data;
                    $("#chooseAddr").html('<div id="province" class="choose" onclick="chooseDistrict(this,1)"> <span>' + district.province + '</span> </div><div id="city" class="choose" onclick="chooseDistrict(this,2)"><span>' + district.city + '</span></div><div id="county" class="choose active" onclick="chooseDistrict(this,3)"><span>请选择</span></div>');
                });
                districtChoose.hide();
                districtChoose[2].style.display = 'block';
            };
            $scope.chooseCounty = function (county) {
                district.county = county.name;
                var name = district.province + district.city + district.county;
                $("#"+districtId).html(name);
                $("#county").find('span')[0].innerHTML = county.name + "";
                click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval")
                $("#"+districtId).parentNode;
                var placeholderColor = document.getElementById(districtId).parentNode.querySelector(".placeholder_color");
                $(placeholderColor).removeClass("placeholder_color ");
                if(districtId=="district"){
                    $scope.merchant.province = district.province;
                    $scope.merchant.city = district.city;
                    $scope.merchant.county = district.county;
                }else if(districtId=="district1"){
                    $scope.merchant.acctProvince = district.province;
                    $scope.merchant.acctCity = district.city;
                }
            }
        };

    }])
   ;